package cn.edu.sdjzu.xg.kcsj.controller.authority;

import cn.edu.sdjzu.xg.kcsj.exception.BysjException;
import cn.edu.sdjzu.xg.kcsj.security.UserAssRole;
import cn.edu.sdjzu.xg.kcsj.security.UserAssRoleService;
import cn.edu.sdjzu.xg.kcsj.util.Condition;
import cn.edu.sdjzu.xg.kcsj.util.ControllerHelper;
import cn.edu.sdjzu.xg.kcsj.util.JSONUtil;
import cn.edu.sdjzu.xg.kcsj.util.Pagination;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/userAssRoleFindAll")
public class UserAssRoleFindAllController extends HttpServlet {
    // 记录器，可以控制日志信息输出到控制台，替代输出语句
    private static final Logger log = LogManager.getLogger(UserAssRoleController.class);

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 将request body转换为JSON字串
        String req_jsonStr = JSONUtil.getJSON(request);
        // 将request body解析为JSONObject对象
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        // 创建JSON对象respMessage_jsonObj，以便往前端响应信息
        JSONObject respMessage_jsonObj = new JSONObject();
        // 如果请求body为空
        if (req_jsonObj == null) {
            log.debug("未收到请求信息！");
            respMessage_jsonObj.put("message", "未收到请求信息！");
            response.getWriter().println(respMessage_jsonObj);
            return;
        }

        try {
            Pagination pagination = ControllerHelper.getPagination(req_jsonObj);
            // 从请求JSONObject对象中读取键“conditionList”对应的值(描述查询条件的JSONArray对象)
            List<Condition> conditionList = ControllerHelper.getConditions(req_jsonObj);
            // 响应多个信息，其中conditionList, pagination两个对象为空也有可能
            this.responseRoles(response, conditionList, pagination);
        } catch (BysjException e) {
            respMessage_jsonObj.put("message", e.getMessage());
            response.getWriter().println(respMessage_jsonObj);
            // 在命令行打印异常信息在程序中出错的位置及原因
            e.printStackTrace();
        } catch (SQLException e) {
            respMessage_jsonObj.put("message", "数据库操作异常");
            response.getWriter().println(respMessage_jsonObj);
            // 在命令行打印异常信息在程序中出错的位置及原因
            e.printStackTrace();
        } catch (Exception e) {
            respMessage_jsonObj.put("message", "网络异常");
            response.getWriter().println(respMessage_jsonObj);
            // 在命令行打印异常信息在程序中出错的位置及原因
            e.printStackTrace();
        }
    }

    // 分页查询+条件查询，响应多个对象
    private void responseRoles(HttpServletResponse response,
                               List<Condition> conditionList_json_str,
                               Pagination pagination)
            throws IOException, SQLException, BysjException {
        List<UserAssRole> userAssRoles = UserAssRoleService.getInstance().findAll(conditionList_json_str, pagination);
        // 创建JSONObject对象resp_jsonObj，以便往前端响应综合信息
        JSONObject resp_jsonObj = new JSONObject();
        resp_jsonObj.put("totalNum", pagination.getTotalNum());
        resp_jsonObj.put("data", userAssRoles);
        // 响应resp_jsonObj到前端
        response.getWriter().println(resp_jsonObj);
    }
}
